package com.example.bookshelf;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.example.bookshelf.data.BookItem;
import com.example.bookshelf.data.BookLabel;
import com.example.bookshelf.data.Bookshelf;

import java.util.ArrayList;
import java.util.Objects;

public class BookDetailsActivity extends AppCompatActivity {
    static final int RESULT_CODE_CHANGED = 0;
    static final int RESULT_CODE_NO_CHANGED = 1;

    private ArrayList<BookLabel> bookLabels;
    private ArrayList<Bookshelf> bookshelves;
    private BookItem bookItem;
    private int position;
    private boolean isChange;

    Toolbar mToolbar;

    @SuppressLint("NotifyDataSetChanged")
    private final ActivityResultLauncher<Intent> inputDataLaunch = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), result -> {
                if (null != result) {
                    Intent intent = result.getData();
                    if (result.getResultCode() == EditActivity.RESULT_CODE_SUCCESS) {
                        assert intent != null;
                        Bundle bundle = intent.getExtras();
                        bookItem = bundle.getParcelable("bookItem");
                        bookshelves = bundle.getParcelableArrayList("bookshelves");
                        bookLabels = bundle.getParcelableArrayList("bookLabels");
                        UpdateData();
                    }

                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_details);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        this.bookLabels = bundle.getParcelableArrayList("bookLabels");
        this.bookshelves = bundle.getParcelableArrayList("bookshelves");
        this.bookItem = bundle.getParcelable("bookItem");
        this.position = bundle.getInt("position");

        assert bookLabels!=null;

        Log.v("date", "position" + String.valueOf(position));

        initToolBar();
        UpdateData();
    }

// -------------------------------------------------------------------------------------------------
    // ###########
    // 更新显示的数据
    // ###########
    private void UpdateData() {
        mToolbar.setTitle(bookItem.getTitle());

        TextView textViewAuthor = findViewById(R.id.textView_details_author);
        TextView textViewTranslator = findViewById(R.id.textView_details_translator);
        TextView textViewPress = findViewById(R.id.textView_details_press);
        TextView textViewDate = findViewById(R.id.textView_details_date);
        TextView textViewReadingState = findViewById(R.id.textView_details_reading_state);
        TextView textViewBookshelf = findViewById(R.id.textView_details_bookshelf);
        TextView textViewNotes = findViewById(R.id.textView_details_notes);
        TextView textViewLabel = findViewById(R.id.textView_details_label);
        TextView textViewUrl = findViewById(R.id.textView_details_url);
        TextView textViewISBN = findViewById(R.id.textView_details_isbn);

        textViewAuthor.setText(bookItem.getAuthor());
        textViewTranslator.setText(bookItem.getTranslator());
        textViewPress.setText(bookItem.getPress());
        textViewDate.setText(bookItem.getDate());
        textViewReadingState.setText(bookItem.getReadingStatus());
        textViewISBN.setText(bookItem.getISBN());

        String bookshelfName = bookItem.getBookshelfName();
        if (bookshelfName!=null && !bookshelfName.equals(""))
            textViewBookshelf.setText(bookshelfName);
        else
            textViewBookshelf.setText("默认书架");

        String bookLabel = bookItem.getLabels();
        if (bookLabel!=null && !bookLabel.equals(""))
            textViewLabel.setText(bookLabel);
        else
            textViewLabel.setText("未设置标签");

        textViewNotes.setText(bookItem.getNotes());
        textViewUrl.setText(bookItem.getUrl());

    }


// -------------------------------------------------------------------------------------------------
    // ############
    // 初始化toolBar
    // ############
    private void initToolBar() {
        mToolbar = findViewById(R.id.toolbar_details);
//        setSupportActionBar(mToolbar);
        setSupportActionBar(mToolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
        mToolbar.setTitle(bookItem.getTitle());
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();
            }
        });
    }

    private void goBack() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable("bookItem", bookItem);
        bundle.putParcelableArrayList("bookshelves", this.bookshelves);
        bundle.putParcelableArrayList("bookLabels", this.bookLabels);
        bundle.putInt("position", position);
        intent.putExtras(bundle);
        if (isChange)
            setResult(RESULT_CODE_CHANGED, intent);
        else
            setResult(RESULT_CODE_NO_CHANGED, intent);
        BookDetailsActivity.this.finish();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_details_toolbar, menu);
        return true;
    }

    int yourChoice;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        if (item.getItemId() == R.id.item_details_edit) {
            isChange = true;

            Intent intent = new Intent(BookDetailsActivity.this, EditActivity.class);
            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("bookshelves", this.bookshelves);
            bundle.putParcelableArrayList("bookLabels", this.bookLabels);
            bundle.putInt("mode", EditActivity.MODE_EDIT);
            bundle.putParcelable("bookItem", bookItem);
            intent.putExtras(bundle);
            inputDataLaunch.launch(intent);
        }
        return true;
    }

}